from flask import Flask
from flask_restful import Resource, Api

def decorator1(func):
    def wrapper(*args,**kwargs):
        print('decorator1')
        return func(*args, **kwargs)
    return wrapper

def decorator2(func):
    def wrapper(*args,**kwargs):
        print('decorator2')
        return func(*args, **kwargs)
    return wrapper

app = Flask(__name__)
api = Api(app)


class Index(Resource):
    # 1.以列表的方式，赋值给method_decorators类属性。对所有请求方法有效
    # method_decorators = [decorator1, decorator2]
    # 2.以字典的方式，赋值给method_decorators。分别对请求方法生效
    method_decorators = {
        'get': [decorator1,decorator2],
        'post': [decorator1]
    }
    def get(self):
        return {'msg': 'get'}

    def post(self):
        return {'msg': 'post'}

api.add_resource(Index, '/')

if __name__ == "__main__":
    app.run(debug=True,host='0.0.0.0', port=5000)
